package arrays;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 开发商买土地（前缀和）
 * User: Li_yizYa
 * Date: 2025—03—23
 * Time: 23:42
 */
public class Solution7 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int sum = 0;
        int[][] arr = new int[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = scan.nextInt();
                sum += arr[i][j];
            }
        }

        int count = 0;
        int ret = Integer.MAX_VALUE;
        //统计行
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                count += arr[i][j];
                if (j == m - 1) {
                    ret = Math.min(ret, Math.abs(sum - 2 * count));
                }
            }
        }

        count = 0;
        //统计列
        for (int j = 0; j < m; j++) {
            for (int i = 0; i < n; i++) {
                count += arr[i][j];
                if (i == n - 1) {
                    ret = Math.min(ret, Math.abs(sum - 2 * count));
                }
            }
        }
        System.out.println(ret);
        scan.close();
    }


    public static void main1(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int[][] arr = new int[n][m];
        int sum = 0;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = scan.nextInt();
                sum += arr[i][j];
            }
        }

        //统计横向
        int[] horization = new int[n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                horization[i] += arr[i][j];
            }
        }

        //统计纵向
        int[] vertical = new int[m];
        for (int j = 0; j < m; j++) {
            for (int i = 0; i < n; i++) {
                vertical[j] += arr[i][j];
            }
        }

        int horizationCut = 0;
        int ret = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            horizationCut += horization[i];
            ret = Math.min(ret, Math.abs((sum - horizationCut) - horizationCut));
        }

        int verticalCut = 0;
        for (int j = 0; j < m; j++) {
            verticalCut += vertical[j];
            ret = Math.min(ret, Math.abs((sum - verticalCut) - verticalCut));
        }

        System.out.println(ret);
        scan.close();
    }
}
